package team.stride.database.crud.results;

import java.util.List;

import team.stride.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ResultsAdapter extends BaseAdapter
{
	private BDResultsActivity activity;
	private List<ResultsItem> resultsList;

	public ResultsAdapter(BDResultsActivity activity, List<ResultsItem> mapslist)
	{
		this.activity = activity;
		this.resultsList = mapslist;
	}

	public int getCount()
	{
		return resultsList.size();
	}

	public ResultsItem getItem(int position)
	{
		return resultsList.get(position);
	}

	public long getItemId(int position)
	{
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup viewGroup)
	{
		final ResultsItem entry = resultsList.get(position);
		if (convertView == null)
		{
			LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.bd_results_row, null);
		}
		
		TextView resultsName = (TextView) convertView.findViewById(R.id.resultsName);
		resultsName.setText(entry.getName());

		TextView resultsUsername = (TextView) convertView.findViewById(R.id.resultsUsername);
		resultsUsername.setText(entry.getUsername());

		TextView resultsLength= (TextView) convertView.findViewById(R.id.resultsLength);
		resultsLength.setText(entry.getLength() + " Km");

		TextView resultsDate= (TextView) convertView.findViewById(R.id.resultsDate);
		resultsDate.setText(entry.getStringDate());

		TextView resultsLaps = (TextView) convertView.findViewById(R.id.resultsLaps);
		resultsLaps.setText("" + entry.getLaps());
		
		TextView resultsTime= (TextView) convertView.findViewById(R.id.resultsTime);
		resultsTime.setText(entry.getTime() + " sec");
		
		TextView resultsSpeed = (TextView) convertView.findViewById(R.id.resultsSpeed);
		resultsSpeed.setText(entry.getTopSpeed() + "Km/h");

		
		TextView resultsDetails = (TextView) convertView.findViewById(R.id.resultsDetails);
		resultsDetails.setText("" + entry.getDetails());

		ImageView resultsImg = (ImageView) convertView.findViewById(R.id.resultsImg);
		resultsImg.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				activity.showDialog(BDResultsActivity.MODIFY_DIALOG, entry);
			}
		});
		return convertView;
	}
}
